<?php
class RegisterControllerCore extends FrontController
{
	public $ssl = true;
	public $php_self = 'register.php';

	public function preProcess()
	{
		parent::preProcess();

		if ($back = Tools::getValue('back'))
			self::$smarty->assign('back', Tools::safeOutput($back));
		
		if (Tools::isSubmit('submitAccount')){
			self::submitAccount();
		}
		if (Tools::isSubmit('sendPhoneCode')){
			self::sendPhoneCode();
		}
		if (Tools::isSubmit('validatePhoneMobile')){
			self::validatePhoneMobile();
		}
		if (Tools::isSubmit('ajax'))
		{
			$return = array(
					'hasError' => !empty($this->errors),
					'errors' => $this->errors,
					'token' => Tools::getToken(false)
			);
			die(Tools::jsonEncode($return));
		}
	}
	
	private function sendPhoneCode(){
		if(self::$cookie->isLogged()){
			$phoneValidation = new PhoneValidation();
			$phoneValidation->id_customer = self::$cookie->id_customer;
			$phoneValidation->code = sprintf('%4d',rand(0,9999));
			$phoneValidation->ip_address = ip2long($_SERVER['REMOTE_ADDR']);
			if($phoneValidation->ip_address==false){
				$phoneValidation->ip_address=0;
			}
			$this->errors = array_unique(array_merge($this->errors, $phoneValidation->validateControler()));
			if (!sizeof($this->errors)){
				$phoneValidation->code = sprintf('%4d',rand(0,9999));
				if(!$phoneValidation->add()){
					$this->errors[] = Tools::displayError('An error occurred while creating validation code.');
				}
				else{
					//TODO send out SMS message
				}
			}
		}
	}
	
	private function validatePhoneMobile(){
		if(self::$cookie->isLogged()){
			$phoneValidation = new PhoneValidation();
			$id_customer = self::$cookie->id_customer;
			$phone_mobile = Tools::getValue('phone_mobile');
			$code = Tools::getValue('code');
			if(PhoneValidation::checkPhoneCode($id_customer,$phone_mobile,$code)){
				$customer = new Customer();
				$customer->id = self::$cookie->id_customer;
				$this->errors[] = $customer->updatePhoneMobile($phone_mobile);
				if(!sizeof($this->errors)){
					if ($back = Tools::getValue('back'))
						Tools::redirect($back);
					Tools::redirect('my-account.php');
				}
			}
			else{
				$this->errors[] = Tools::displayError('Invalidate phone validation code.');
				self::$smarty->assign('confirmation', 1);
			}
		}
	}
	
	private function submitAccount(){
		if(Customer::userNameExists($user_name =Tools::getValue('user_name'))){
			$this->errors[] = Tools::displayError('The account already has been registered, please request a new one.');
		}
		else{
			/* Preparing customer */
			$customer = new Customer();
			$this->errors = array_unique(array_merge($this->errors, $customer->validateControler()));
		}
		if (!sizeof($this->errors)){
			$customer->active = 1;
			if (!$customer->add(true, false)){
				$this->errors[] = Tools::displayError('An error occurred while creating your account.');
			}
			else{
				self::$smarty->assign('confirmation', 1);
				self::$cookie->id_customer = (int)($customer->id);
				self::$cookie->user_name = $customer->user_name;
				self::$cookie->passwd = $customer->passwd;
				self::$cookie->logged = 1;
			}
		}
		
	}
	
	public function displayContent()
	{	
		parent::displayContent();
		self::$smarty->display(_PS_THEME_DIR_.'register.tpl');
	}
}
